package com.zou.application.utils;

import java.io.IOException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;

import com.alibaba.fastjson.JSONObject;

/**
 * org.apache.commons.httpclient.HttpClient
 * 基于apache.common包下的http请求
 * @author zhaieryuan
 * @date 2018年1月9日 上午8:59:28
 */
public class HttpMethodUtils {
	
	
	/**
	 * 内嵌类实现获取单利模式的HttpClient
	 * @author zhaieryuan
	 * @date 2018年1月9日 上午9:22:01
	 */
	public static class GetHttpClient{
		/**
		 * 声明HttpClient实例
		 */
		private static HttpClient http;
		
		/**
		 * 定义对象获取的钥匙，基于线程同步
		 */
		private static String key="httpKey";
		
		/**
		 * 声明私有的构造方法，外部不允许实例创建对象
		 * @author zhaieryuan
		 * @date 2018年1月9日 上午9:37:38
		 */
		private GetHttpClient(){}
		
		/**
		 * 定义静态方法，获取HttpClient对象
		 * @return
		 * @author zhaieryuan
		 * @date 2018年1月9日 上午9:38:26
		 */
		public static HttpClient getClient(){
			if(http == null){
				synchronized (key) {
					return new HttpClient();
				}
			}
			return null;
		}
	}
	
	/**
	 * 发送http POST请求
	 * @param url
	 * @param param
	 * @return
	 * @author zhaieryuan
	 * @date 2018年1月9日 上午9:24:29
	 */
	@SuppressWarnings("deprecation")
	public static String doPost(String url,Map<String, Object> param) throws HttpException{
		HttpClient http = GetHttpClient.getClient();
		@SuppressWarnings("unused")
		String urlParam="";
		String respStr = "";
		try {
			PostMethod postMethod= new PostMethod(url);
			postMethod.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
			postMethod.getParams().setContentCharset("utf-8");
			for(Map.Entry<String, Object> item : param.entrySet()){
				JSONObject data = new JSONObject();
				data.put(item.getKey(), item.getValue());
				if(!data.isEmpty()){
					NameValuePair[] requestData = { new NameValuePair("requestData", URLEncoder.encode(data.toJSONString(),"UTF-8"))};
					postMethod.setRequestBody(requestData);
				}else{
					postMethod.setRequestBody("");
				}
			}
			postMethod.getParams().setContentCharset("utf-8");
			http.getHttpConnectionManager().getParams().setConnectionTimeout(30000);
			http.executeMethod(postMethod);
			respStr = URLDecoder.decode(postMethod.getResponseBodyAsString(),"UTF-8");
		} catch (IOException e) {
			e.printStackTrace();
			return "";
		}
		return respStr;
	}
	
	/**
	 * 发送Get请求
	 * @param url
	 * @param param
	 * @return
	 * @author zhaieryuan
	 * @date 2018年1月9日 上午9:24:47
	 */
	public static String doGet(String url,Map<String, Object> param){
		HttpClient http = GetHttpClient.getClient();
		String urlParam="";
		String respStr = "";
		try {
			for(Map.Entry<String, Object> item : param.entrySet()){
				urlParam += item.getKey()+"="+URLEncoder.encode(item.getValue().toString(),"UTF-8")+"&";
			}
			url=url+"?"+urlParam;
			GetMethod getMethod = new GetMethod(url);
			getMethod.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
			getMethod.getParams().setContentCharset("utf-8");
			http.getHttpConnectionManager().getParams().setConnectionTimeout(30000);
			http.executeMethod(getMethod);
			respStr = getMethod.getResponseBodyAsString();
		} catch (IOException e) {
			e.printStackTrace();
			return "";
		}
		return respStr;
	}
	
	
	
	/**
	 * 测试基于微信token
	 * @param args
	 * @author zhaieryuan
	 * @date 2018年1月9日 上午10:51:30
	 */
	public static void main(String[] args) {
		String url = "http://127.0.0.1:8080/user/login";
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("username", "solar");
		param.put("psassword", "123456");
		String data = doGet(url,param);
		System.out.println(data);
		JSONObject token = JSONObject.parseObject(data);
		System.out.println(token);
	}
	
	
}
